#pragma warning( push )           // memorize the warning status
#pragma warning (disable:4786)   // disable  identifier was truncated to '255' characters in the debug information


#include "tf_Geom_Object_Manager.h"

TfGeomObjectManager::TfGeomObjectManager()
{

}

TfGeomObjectManager::~TfGeomObjectManager()
{
	
}

int TfGeomObjectManager::Begin()
{
	if(m_idMap.size() == 0 || m_data.size() == 0)
		return -1;
	else
	{
		int i,idx;
		std::map<int,int>::iterator iter;
		iter = m_idMap.begin();
		idx = iter->second;
		for(i = idx; i < m_data.size(); i++)
		{
			if(m_data[i]->IsValid())
			{
				m_curId = m_data[i]->GetId();
				return i;
			}
		}
		return 1e10;
	}
}

int TfGeomObjectManager::End()
{
	if(m_idMap.size() == 0 || m_data.size() == 0)
		return -1;
	else
	{
		int i, idx;
		std::map<int,int>::iterator iter;
		iter = m_idMap.end();
		iter--;
		idx = iter->second;
		for(i = idx; i >= 0; i--)
		{
			if(m_data[i]->IsValid())
				return i;
		}
		return -1;
	}
}

int TfGeomObjectManager::Next()
{
	if(m_idMap.size() == 0 || m_data.size() == 0)
		return -1;
	int i,idx,endIdx;
	idx = m_idMap[m_curId];
	endIdx = End();
	for(i = idx+1; i <= endIdx; i++)
	{
		if(m_data[i]->IsValid())
		{
			m_curId = m_data[i]->GetId();
			return i;
		}
	}
	return 1e10;
	
}

int TfGeomObjectManager::GetCurId()
{
	return m_curId;
}

bool TfGeomObjectManager::Delete(int id)
{
	if(m_idMap[id] == -1 || m_data.size() == 0)
		return false;
	else
	{
		int idx;
		idx = m_idMap[id];
		m_data[idx]->Delete();
	}
	return true;
}

int TfGeomObjectManager::GetNum()
{
	if(m_idMap.size() == 0 || m_data.size() == 0)
		return 0;
	int num = 0;
	int i,bgIdx,edIdx;
	bgIdx = Begin();
	edIdx = End();
	if(bgIdx > -1)
	{
		for(i = bgIdx; i <= edIdx; i++)
		{
			if(m_data[i]->IsValid())
				num++;
		}
		return num;
	}
	return 0;
}

int TfGeomObjectManager::operator [](int id) const
{
	if(m_data.size() == 0)
		return -1;
	std::map<int,int>::const_iterator iter;

	iter = m_idMap.find(id);
	if(iter == m_idMap.end())
 		return -1;
 	else
 		return iter->second;
	
}

void TfGeomObjectManager::AddToMap(int id,int idx)
{
	if(idx == m_data.size()-1)
		AdditionSpace();
	m_idMap.insert(std::pair<int,int>(id,idx));/*may insert error*/
}

int TfGeomObjectManager::GetMaxId()
{
	if(m_idMap.size() == 0 || m_data.size() == 0)
		return 0;

	std::map<int,int>::iterator iter;
	iter = m_idMap.end();
	iter--;
	return iter->first;
}

void TfGeomObjectManager::Compress()
{
//	for(i = 0)
}


#pragma warning( pop )           // memorize the warning status